package com.epcs.model.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.epcs.model.dto.Account;
import com.epcs.model.dto.Comment;
import com.epcs.model.dto.Event;
import com.epcs.util.DBUtil;

public class CommentDAO {
	
	//Thêm 1  Comment
	
	public boolean addComment(Comment comment) {

		Session session = DBUtil.getSession();
		Transaction trans = null;
		try {
			trans = session.beginTransaction();
			session.save(comment);
			trans.commit();	
		} catch (HibernateException e) {
			System.out.println(e.getMessage());
			trans.rollback();
		} 
		finally {
			if (session != null) {
				session.close();
			}
		}

		return true;
	}
	//Get Tat ca Comment Thuoc ve 1 EventId
	
	public List<Comment> getCommentListByEventId(Event event) {

		Session session = DBUtil.getSession();
		try {
			Criteria criteria = session.createCriteria(Comment.class);
			Criterion criterion = Restrictions.eq("event", event);
			criteria.add(criterion);
			List<Comment> item = criteria.list();
			return item;
		} catch (HibernateException e) {
			System.out.println(e.getMessage());
		} 
		finally {
			if (session != null) {
				session.close();
			}
		}

		return null;
	}
	
	public Comment getCommentById(String commentId) {

		Session session = DBUtil.getSession();
		try {
			Criteria criteria = session.createCriteria(Comment.class);
			Criterion criterion = Restrictions.eq("commentId", Integer.parseInt(commentId));
			criteria.add(criterion);
			Comment item = (Comment) criteria.uniqueResult();
			return item;
		} catch (HibernateException e) {
			System.out.println(e.getMessage());
		} 
		finally {
			if (session != null) {
				session.close();
			}
		}

		return null;
	}
	//---deleteComment
	public boolean deleteComment(Comment comment) {

		Session session = DBUtil.getSession();
		Transaction trans = null;
		try {
		    trans = session.beginTransaction();
		    session.delete(comment);
		    trans.commit();
		    return true;
		} catch (HibernateException e) {
		    System.out.println(e.getMessage());
		    trans.rollback();
		} finally {
		    if (session != null) {
			session.close();
		    }
		}

		return false;
	    }

}
